home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / server.arc / SERVER.C < prev    next >
Text File  |  1989-11-29  |  5KB  |  126 lines

  1. /*------------------------------------------------* 
  2.  *     Named Pipes server                         * 
  3.  *                                                * 
  4.  *     Written by: Stephen T. Bunch               * 
  5.  *                 API Development                * 
  6.  *                                                * 
  7.  *     Date:       July 11, 1989                  * 
  8.  *                                                * 
  9.  *------------------------------------------------*/ 
  10. #include   <stdio.h> 
  11. #define    INCL_BASE 
  12. #include   <os2.h> 
  13. #include   <nwcalls.h> 
  14. #include   <malloc.h> 
  15. #include   "server.h" 
  16.  
  17. #define    CLIENT_NUM    4       /* number of threads to create (potentially the 
  18.                                     number of concurrent clients also) */ 
  19. #define    STACK_SIZE    2000    /* stack size for each thread */ 
  20. #define    PIPENAME      "\\PIPE\\TLAPIPE" 
  21.  
  22. HPIPE    pipeHandle[CLIENT_NUM]; 
  23.  
  24.  
  25. /*----------------------------------------------------------------------*/ 
  26. main(int argc,char *argv[]) 
  27.     int err, i; 
  28.     void far Receiver();            /* address of thread routine */ 
  29.     TID threadID[CLIENT_NUM];       /* thread IDs */ 
  30.     USHORT far *stackPtr;           /* ptr to thread's stack */ 
  31.  
  32.     TLAReadIndex(); 
  33.     printf("\tWelcome to the TLA server program.\n"); 
  34.     printf("\tEnter 'down' to down this server.\n\n"); 
  35.  
  36.     /* spin off the threads which will each initiate an instance */ 
  37.     for (i=0; i<CLIENT_NUM; i++) { 
  38.         stackPtr = (WORD far *)malloc(STACK_SIZE) + (char)STACK_SIZE; 
  39.         /* pass a parameter by putting the client number on the stack */ 
  40.         stackPtr--; 
  41.         *stackPtr = i; 
  42.         err = DosCreateThread(Receiver, &threadID[i], (char far *)stackPtr); 
  43.         if (err) 
  44.             printf("DosCreateThread Error : %d\n", err); 
  45.     } 
  46.     CleanUp(); 
  47.  
  48. /*----------------------------------------------------------------------* 
  49.  * CleanUp performs the exiting procedures, disconnecting and closing   * 
  50.  * the pipes.                                                           * 
  51.  *----------------------------------------------------------------------*/ 
  52. CleanUp() 
  53.     BYTE response[20]; 
  54.     int err, i; 
  55.  
  56.     /*  wait until 'down' is indicated  */ 
  57.     do { 
  58.         gets(response); 
  59.     } while (strcmp(response, "down") != 0); 
  60.     TLACloseIndex(); 
  61.  
  62.     /* disconnect all instances of the pipe */ 
  63.     DosEnterCritSec(); 
  64.     for (i=0; i<CLIENT_NUM; i++){ 
  65.         err = DosDisConnectNmPipe(pipeHandle[i]); 
  66.         if (err) 
  67.             printf("DosDisConnectNmPipe (Client %d) Error : %d\n", i, err); 
  68.     } 
  69.  
  70.     /* close the pipe handels */ 
  71.     for (i=0; i<CLIENT_NUM; i++){ 
  72.         err = DosClose(pipeHandle[i]); 
  73.         if (err) 
  74.             printf("DosClose (Client %d) Error : %d\n", i, err); 
  75.     }     
  76.     DosExitCritSec(); 
  77.     DosExit(1, 0); 
  78.  
  79. /*----------------------------------------------------------------------* 
  80.  * Receiver is the server side of the IPC connection.  It makes, reads, * 
  81.  * writes, and closes the pipe until 'down' is entered.                 * 
  82.  *----------------------------------------------------------------------*/ 
  83. void far Receiver(ClientNum) 
  84. int ClientNum; 
  85.     USHORT openMode = 0x0002;                /* Duplex pipe */ 
  86.     USHORT pipeMode = 0x0504;                /* Number of instances allowed */ 
  87.     LONG timeOut = 0;                        /* default time out value */ 
  88.     USHORT bytesWritten, bytesRead; 
  89.     TID threadID; 
  90.     USHORT err; 
  91.     struct COMMBUFFER commBuffer; 
  92.  
  93.     /* create an instance of the named pipe  */ 
  94.     err = DosMakeNmPipe(PIPENAME, &pipeHandle[ClientNum], openMode, pipeMode, 
  95.             sizeof(struct COMMBUFFER), sizeof(struct COMMBUFFER), timeOut); 
  96.     do { 
  97.         /* wait for the user to connect to the named pipe */ 
  98.         err = DosConnectNmPipe(pipeHandle[ClientNum]); 
  99.         printf("DosConnectNmPipe (Client %d) : %d\n", ClientNum, err); 
  100.  
  101.         /* Read from the pipe */ 
  102.         err = DosRead(pipeHandle[ClientNum], &commBuffer, 
  103.                                 sizeof(struct COMMBUFFER), &bytesRead); 
  104.         if (err) 
  105.             printf("DosRead Error : %d\n", err); 
  106.         if ((err = TLAServiceRequest(&commBuffer)) != 0) { 
  107.             strcpy(commBuffer.record.explain,"The TLA requested could not be found"); 
  108.         } 
  109.         err = DosWrite(pipeHandle[ClientNum], &commBuffer, 
  110.                         sizeof(struct COMMBUFFER), &bytesWritten); 
  111.         if (err) 
  112.             printf("DosWrite Error (Client %d) : %d\n", ClientNum, err); 
  113.         DosSleep(500L); 
  114.  
  115.         /*  user end of the pipe has gone away - close this instance  */ 
  116.         err = DosDisConnectNmPipe(pipeHandle[ClientNum]); 
  117.     } while (TRUE); 
  118.  
  119.  
  120.